<template>
  <div class="cascader-compoents-container">
    <div class="city-select" @click.stop="toggleCascader">
      <a-input
        v-model:value="cascaderName"
        :style="{ width: width + 'px', 'text-align': 'left' }"
        placeholder="选择城市"
      />
      <DownOutlined
        style="
          font-size: 12px;
          color: rgba(0, 0, 0, 0.25);
          position: absolute;
          right: 11px;
          top: 10px;
        "
      />
    </div>
    <div class="city-select-content" v-if="cascaderShow" v-click-outside="handleClose">
      <div class="province" :style="{ width: width / 2 + 'px' }" v-if="province.length > 0">
        <div class="item" v-for="item in province" :key="item.value" @click="onSelected(item, 0)">
          <div>{{ item.label }}</div>
          <RightOutlined style="font-size: 12px; color: rgba(0, 0, 0, 0.25)" />
        </div>
      </div>
      <div class="city" :style="{ width: width / 2 + 'px' }" v-if="city.length > 0">
        <div class="item" v-for="item in city" :key="item.value" @click="onSelected(item, 1)">
          <div>{{ item.label }}</div>
          <RightOutlined style="font-size: 12px; color: rgba(0, 0, 0, 0.25)" />
        </div>
      </div>
      <div class="district" :style="{ width: width / 2 + 'px' }" v-if="district.length > 0">
        <div class="item" v-for="item in district" :key="item.value" @click="onSelected(item, 2)">
          <div>{{ item.label }}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, toRefs, watch } from 'vue'
import * as addressApi from '@/api/address'

const props = defineProps({
  width: {
    type: [Number, String],
    default: 300,
  },
  placeholder: {
    type: String,
    default: '选择城市',
  },
  modelValue: {
    type: Array,
    default: () => [],
  },
})
const { width, modelValue, placeholder } = toRefs(props)
const cascaderName = ref('')
const values = ref([])
const selectedId = ref([...modelValue.value])
const province = ref([])
const city = ref([])
const district = ref([])
const cascaderShow = ref(false)
const emits = defineEmits(['update:modelValue', 'onSelected'])
const updateCascaderName = () => {
  cascaderName.value = selectedId.value.map(item => item.label).join(' / ') || ''
}
// 监听 modelValue 变化以实现回显
watch(
  modelValue,
  async newVal => {
    selectedId.value = [...newVal]
    updateCascaderName()

  },
  { immediate: true },
)

const vClickOutside = {
  mounted(el: HTMLElement, binding: any) {
    el.clickOutsideEvent = (e: Event) => {
      if (!(el === e.target || el.contains(e.target as Node))) {
        binding.value(e)
      }
    }
    document.body.addEventListener('click', el.clickOutsideEvent)
  },
  unmounted(el: HTMLElement) {
    document.body.removeEventListener('click', el.clickOutsideEvent)
  },
}

const toggleCascader = () => {
  cascaderShow.value = !cascaderShow.value
}

const handleClose = () => {
  cascaderShow.value = false
}
// 添加点击外部关闭逻辑
const handleClickOutside = (e: MouseEvent) => {
  //   const container = document.querySelector(`.cascader-compoents-container`)
  if (!e.currentTarget?.contains(e.target as Node)) {
    cascaderShow.value = false
  }
}
const getCityData = async (parentId: any, index: any) => {
  try {
    let { state, data, message: msg } = await addressApi.getChannelAreaList({ parentId })
    if (state === 200) {
      if (index == 0) {
        province.value = data.map((item: any) => {
          return {
            value: item.id,
            label: item.name,
            //   children: [],
          }
        })
        district.value = []
      } else if (index == 1) {
        city.value = data.map((item: any) => {
          return {
            value: item.id,
            label: item.name,
          }
        })
      } else if (index == 2) {
        district.value = data.map((item: any) => {
          return {
            value: item.id,
            label: item.name,
          }
        })
      }
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
}
const onSelected = (item, index) => {
  console.log(item, index, 'item, index')
  if (index == 0) {
    city.value = []
    district.value = []
    selectedId.value[0] = item
  }
  if (index == 1) {
    selectedId.value[1] = item
  }
  if (index == 2) {
    selectedId.value[2] = item
    cascaderShow.value = false
  }

  cascaderName.value = selectedId.value.map(item => item.label).join(' / ')
  emits('onSelected', selectedId.value)
  emits('update:modelValue', [...selectedId.value])
  getCityData(item.value, index + 1)
}
onMounted(() => {
  document.addEventListener('click', handleClickOutside)
  getCityData(0, 0)
})
onBeforeUnmount(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>
<style lang="less" scoped>
.cascader-compoents-container {
  position: relative;
  .city-select {
    position: relative;
  }
  .city-select-content {
    position: absolute;
    display: flex;
    justify-content: space-around;
    left: 5px;
    top: 40px;
    z-index: 99;
    .province,
    .city,
    .district {
      background-color: #fff;
      height: 200px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
      overflow-y: auto;
      border-radius: 5px;
      padding: 5px 0;
      transition: all, 0.3s;

      .item {
        display: flex;
        font-size: 13px;
        justify-content: space-between;
        align-items: center;
        padding: 0 10px;
        height: 35px;
        line-height: 30px;
        cursor: pointer;
        transition: all, 0.3s;

        &:hover {
          background-color: #f0f0f0;
        }
      }
    }
    .city,
    .district {
      margin-left: 10px;
    }
  }
}
</style>
